package com.elitedatai.mchtest.wayline.repository;

import com.elitedatai.mchtest.wayline.model.entity.WaylineFileEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

public interface WaylineFileRepository extends JpaRepository<WaylineFileEntity, Long>, JpaSpecificationExecutor<WaylineFileEntity> {

    @Query("DELETE FROM WaylineFileEntity w WHERE w.workspaceId = :workspaceId AND w.waylineId = :waylineId")
    int deleteByWorkSpaceIdAndWaylineId(String workspaceId, String waylineId);

    @Query("SELECT w.name FROM WaylineFileEntity w WHERE w.workspaceId = :workspaceId AND w.name IN :names")
    List<String> findDuplicateNames(@Param("workspaceId") String workspaceId, @Param("names") List<String> names);

    @Modifying
    @Transactional
    @Query("UPDATE WaylineFileEntity w SET w.favorited = :isFavorite WHERE w.workspaceId = :workspaceId AND w.waylineId IN :waylineIds")
    int markFavorite(@Param("workspaceId") String workspaceId, @Param("waylineIds") List<String> waylineIds, @Param("isFavorite") Boolean isFavorite);

    WaylineFileEntity findByWorkspaceIdAndWaylineId(String workspaceId, String waylineId);
}
